<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="学校名称" prop="schoolId">
        <FusionSelect type="school" v-model="queryParams.schoolId" />
      </el-form-item>
      <el-form-item label="楼座名称" prop="buildId">
        <FusionSelect type="build" v-model="queryParams.buildId" />
      </el-form-item>
      <el-form-item label="查询区间">
        <el-date-picker
          v-if="queryParams.range == 2"
          v-model="queryParams.dtList"
          type="monthrange"
          range-separator="-"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM"
        />
        <el-date-picker
          v-else
          v-model="queryParams.dtList"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="统计类型">
        <el-radio-group v-model="queryParams.range" @input="handleRadio">
          <el-radio :label="2">月用量</el-radio>
          <el-radio :label="1">日用量</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:area:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:area:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:area:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:area:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>
    <div class="charts-data" ref="chartsData">

    </div>
  </div>
</template>
<script>
import { statistics } from '@/api/analyse/electricity'
import FusionSelect from '@/components/FusionSelect'
import * as echarts from 'echarts';
import moment from 'moment'
import { mapGetters } from 'vuex'
export default {
  components: {
    FusionSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        buildId: null,
        dtList: [],
        range: 2
      },
      // 表单参数
      form: {},
      myChart: null,
      option: {},
    }
  },computed: {
    ...mapGetters([
      'school',
    ]),
  },
  mounted() {
    this.queryParams.dtList = [
      moment().subtract(12,'months').startOf('month').format('YYYY-MM'),
      moment().subtract(1,'months').startOf('month').format('YYYY-MM')
    ]
    this.init().then(() => {
      this.getList()
    })
    window.addEventListener('resize', this.resize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resize)
  },
  methods: {
    handleRadio(val) {
      if(val == 1) {
        this.queryParams.dtList.splice(0, this.queryParams.dtList.length, ...[
          moment().subtract(2,'months').startOf('month').format('YYYY-MM-DD'),
          moment().subtract(1,'months').startOf('month').format('YYYY-MM-DD')
        ])
        console.log(this.queryParams.dtList)
      } else {
        this.queryParams.dtList = [
          moment().subtract(12,'months').startOf('month').format('YYYY-MM'),
          moment().subtract(1,'months').startOf('month').format('YYYY-MM')
        ]
      }
    },
    async init() {
      this.myChart = echarts.init(this.$refs.chartsData)
      this.option = {
        title: {
          show: true,
          text: `${this.school}用电分析`,
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          backgroundColor: '#fff',
          borderColor: '#409EFF',
          borderWidth: 1,
          textStyle: {
            corlo: '#000',
            fontSize: 12
          },
          formatter: (params) => {
            return `
              <div>${params[0].name}<div>
              <div><span style="color: #409EFF">用电量(kwh)：</span>${params[0].data}</div>
            `
          }
        },
        xAxis: {
          type: 'category',
          data: [],
          minInterval: 1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [],
            type: 'line',
          }
        ],
        grid: {
          top: 40,
          left: 50,
          right: 20,
          bottom: 40
        },
        // dataZoom: [
        //   {
        //     type: 'slider',
        //     show: false,
        //     xAxisIndex: [0],
        //     start: 0,
        //     end: 30,
        //     bottom: '0%',
        //     height: 10,
        //     fillerColor: '#337AB7',
        //     borderColor: 'transparent',
        //     backgroundColor: 'white',
        //     startValue: 0,
        //     endValue: 3,
        //     showDetail: false,
        //   }
        // ]
      }
      this.myChart.setOption(this.option);
    },
    resize() {
      this.myChart.resize()
    },
    getList() {
      statistics(this.queryParams).then(response => {
        this.option.xAxis.data = []
        this.option.series[0].data = []
        if(response.data.length) {
          response.data.map(item => {
            this.option.xAxis.data.push(item.date)
            this.option.series[0].data.push(item.useEq)
          })
        } 
        setTimeout(() => {
          this.myChart.setOption(this.option);
        },200)
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.dtList = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  }
}
</script>
<style lang="scss" scoped>
.charts-data {
  width: 100%;
  height: calc(100vh - 220px);
}
</style>